//TODO: License Header.

/*
This code is licensed under the BSD license.

*/
package analysis.metrics

import primitives.cluster.ClusterHead
import primitives.graph.Node
import search.util.TreeLoader2
import static Math.min
import primitives.cluster.*;
import primitives.graph.*;

class IntraExtraEdgeMetric /* extends Metric */{

	def static outEdges(IClusterLevel c){
        return edgesFilteredBy(c,{
                trans -> 
                !c.nodes.contains(trans.destinationNode)
          
        });
    }

	def static intraEdges(IClusterLevel c){
		return edgesFilteredBy(c,{
                trans -> 
                c.nodes.contains(trans.destinationNode)
          
        });
	}

	def static edgesFilteredBy(IClusterLevel c, clj){
		Collection<Transition> nodes = []
        c.nodes.each{
            node ->
            nodes.addAll(node.getTransitionsAsT().findAll(clj))
        }
        return nodes
	}
	
	static def evaluate(ClusterHead tree){
		def intra = 0;
		def extra = 0;
		Queue smods = new LinkedList(tree.children)
        while(!smods.isEmpty()){

            def mod = smods.remove()
            
				extra += outEdges(mod).size()
				intra += intraEdges(mod).size()
                
                if(mod.encapsulates()){
                    //smods.addAll(mod.children)
                }
            }

        

		return [intra,extra]


	}
	
	@Deprecated
	public static void main(String[] args){
		def filename = args[0]

		ClusterHead tree = TreeLoader2.loadTreeFromSerial(new File(filename))
		def(intra,extra) = evaluate(tree);
		def nodec = tree.nodes.size();
		
		Graph g = new Graph(tree.nodes);
		

		
		print("$intra, $extra")
		
	}
	
}